import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_give/repository/api.dart';
import 'package:flutter_give/utils/LogUtils.dart';
import 'package:oktoast/oktoast.dart';
import '../../route/route_utils.dart';
import '../Home/home_page.dart';
import '../Home/home_vm.dart';
import '../tab_page.dart';

/// 修改设备名称页面
class EqpagesavePage extends StatefulWidget {
  final String deviceId;
  final String deviceName;
  final String ID;

  const EqpagesavePage({super.key, required this.deviceId,required this.deviceName,required this.ID});

  @override
  State<StatefulWidget> createState() {
    return _EqpagesavePageState();
  }
}

class _EqpagesavePageState extends State<EqpagesavePage> {
  // 获取类
  HomeViewModel viewModel = HomeViewModel();
  final TextEditingController _titlesave = TextEditingController(text: "");
  late String _deviceId;
  late String _deviceName;
  late String _ID;

  @override
  void initState() {
    super.initState();
    _deviceId = widget.deviceId;
    _deviceName = widget.deviceName;
    _ID = widget.ID;
    _titlesave.text = _deviceName;

    LogUtils.w("设备ID: $_deviceId");

    _titlesave.addListener(_onTextChanged);

    SystemChrome.setSystemUIOverlayStyle(const SystemUiOverlayStyle(
      statusBarColor: Color(0xFFffffff),
      statusBarIconBrightness: Brightness.dark,
    ));
  }

  @override
  void dispose() {
    _titlesave.removeListener(_onTextChanged);
    super.dispose();
  }

  void _onTextChanged() {
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Color(0xFFF2F3F5),
      appBar: AppBar(
        leading: IconButton(
          icon: Icon(Icons.arrow_back_ios),
          onPressed: () {
            Navigator.of(context).pop();
          },
        ),
        automaticallyImplyLeading: false,
        title: Text(
          "修改设备名称",
          style: TextStyle(
            fontSize: 17,
            fontFamily: "PingFang SC",
            fontWeight: FontWeight.w600,
          ),
        ),
        centerTitle: true,
        actions: [
          TextButton(
            child: Text(
              "保存",
              style: TextStyle(
                color: Colors.green,
                fontSize: 16,
                fontFamily: "PingFang SC",
                fontWeight: FontWeight.w600,
              ),
            ),
            onPressed: () async {
              if (_titlesave.text == null || _titlesave.text.isEmpty) {
                showToast("请输入设备名称");
              } else {
                // final response =
                // await SpecService.rename(_deviceId, "${_titlesave.text}");
                //
                // if (!context.mounted) return;
                // if (!response.success) {
                //   SnackTips.show(context, "Error: ${response.message}");
                //
                // } else{
                //   showToast("重命名成功");
                //   HomePageState.globalKeytwo.currentState?.reloadData();
                //   RouteUtils.pushAndRemoveUntil(context, TabPage());
                // }


                bool swticheq = await Api.instance.eqsavetitle(id: _deviceId,name: _titlesave.text);

                if (swticheq) {
                  showToast("重命名成功");
                  HomePageState.globalKeytwo.currentState?.reloadData();
                  RouteUtils.pushAndRemoveUntil(context, TabPage());
                }

              }
            },
          ),
        ],
      ),
      body: SingleChildScrollView(
        child: Container(
          child: Padding(
            padding: EdgeInsets.all(16), // 添加内边距
            child: TextField(
              controller: _titlesave,
              decoration: InputDecoration(
                hintText: "在此设置设备名称",
                border: OutlineInputBorder(
                  borderRadius: BorderRadius.circular(10), // 设置圆角半径
                  borderSide: BorderSide.none, // 去除边框线
                ),
                filled: true,
                // 启用背景填充
                fillColor: Colors.white,
                // 设置背景颜色为白色
                suffixIcon: _titlesave.text.isNotEmpty
                    ? IconButton(
                        icon: Icon(Icons.clear),
                        onPressed: () {
                          setState(() {
                            _titlesave.clear();
                          });
                        },
                      )
                    : null,
                contentPadding: EdgeInsets.symmetric(
                    vertical: 0, horizontal: 15), // 设置内容内边距
              ),
              cursorColor: Color(0xFF00c888),
            ),
          ),
        ),
      ),
    );
  }
}
